package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import dao.FavouriteDAO;
import dao.RestaurantDAO;

import beans.Favourite;
import beans.Restaurant;
import beans.User;
@Controller
public class FavouriteController {

	private RestaurantDAO restaurantDAO;
	private FavouriteDAO favouriteDAO;
	private LoggedUser loggedUser;	
	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}

	

	public void setLoggedUser(LoggedUser loggedUser) {
		this.loggedUser = loggedUser;
	}



	public void setFavouriteDAO(FavouriteDAO favouriteDAO) {
		this.favouriteDAO = favouriteDAO;
	}



	

	@RequestMapping(value = "/favouriteRestuarantId={restaurantId}")
	public ModelAndView favourite(@PathVariable("restaurantId") int id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Restaurant restaurant = restaurantDAO.findRestaurantById(id);
		User user = loggedUser.getLoggedUser();
		Favourite favourite = favouriteDAO.findFavourite(user, restaurant);
		if(favourite==null){
			favourite = new Favourite();
			favourite.setRestaurant_id(id);
			favourite.setUsername(user.getUsername());
			favouriteDAO.saveFavourite(favourite);
		}
		else{
			favouriteDAO.deleteFavourite(favourite);
		}
		return new ModelAndView("redirect:restaurantid="+id+".htm");
	}
}
